package com.zjitc.P3.demo1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/13 16:00
 * @description:
 */
@Aspect  //切面类
public class AspectDemo {

  /**
   * 通知的5种类型Advice

   ·Before-在方法被调用之前调用通知。

   ·After-在方法被调用之后调用通知，无论方法是否执行成功。

   ·After-returning-在方法成功执行之后调用通知。

   ·After-throwing-在方法抛出异常后调用通知。

   ·Around-方法被调用之前和调用之后各执行一次自定义的行为，通知把被通知方法“包裹”起来
   */

    @Pointcut("execution(* com.zjitc.p3.demo1.A.a())")
    public void pointcutA() {}

    @Pointcut("execution(* com.zjitc.p3.demo1.A.b())")
    public void pointcutB() {}
    /**
     * 1. Pointcut
     * 2. Advice
     */
    @Before("pointcutA()")
    public void beforeA() {
        System.out.println("init before a");
    }

    @After("pointcutA()")
    public void afterA() { System.out.println("destroy after a");}

    @AfterThrowing("pointcutB()")
    public void whenException() {
        System.out.println("error happened");
    }

    @Around("execution(* com.zjitc.p3.demo1.A.c())")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println(jp.getSignature().getName());
        jp.proceed();
    }

}
